package cn.hs.ph.servlet;

import cn.hs.ph.dao.ShopDao;
import cn.hs.ph.dao.impl.ShopDaoImpl;
import cn.hs.ph.entity.Shop;
import cn.hs.ph.util.Result;
import com.alibaba.fastjson.JSON;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/shop")
public class ShopServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        ShopDao shopDao = new ShopDaoImpl();
        //查询
        if("selectAll".equals(action)){
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String keyword = req.getParameter("keyword");
            String phone = req.getParameter("phone");
            String shopStatus = req.getParameter("shopStatus");
            //参数封装

            Map<String, String> params = new HashMap<>();
            params.put("startTime", startTime);
            params.put("endTime", endTime);
            params.put("pageNum", pageNum);
            params.put("pageSize", pageSize);
            params.put("keyword", keyword);
            params.put("phone", phone);
            params.put("shopStatus", shopStatus);
            // 总条数
            int total = shopDao.selectCount(params);
            List<Shop> list = shopDao.selectAll(params);
            Map<String,Object> resMap =new HashMap<>();
            resMap.put("list",list);
            resMap.put("total",total);
            Result data = Result.success(resMap);
            resp.getWriter().write(JSON.toJSONString(data));
            //添加
        }else if("add".equals(action)){
            String shopAddress = req.getParameter("shopAddress");
            String shopCity = req.getParameter("shopCity");
            String shopDescribe = req.getParameter("shopDescribe");
            String shopDistrict = req.getParameter("shopDistrict");
            String shopImage = req.getParameter("shopImage");
            String shopLatitude = req.getParameter("shopLatitude");
            String shopLongitude = req.getParameter("shopLongitude");
            String shopName = req.getParameter("shopName");
            String shopPhone = req.getParameter("shopPhone");
            String shopProvince = req.getParameter("shopProvince");
            Shop shop = new Shop(shopName,shopDescribe,shopImage,shopPhone,
                    shopProvince,shopCity,shopDistrict,shopAddress,shopLongitude,shopLatitude);
            int num = shopDao.insert(shop);
            Result res=null;
            if(num!=0){
                res=Result.success();
            }else {
                res=Result.error();
            }
            resp.getWriter().write(JSON.toJSONString(res));
        }
        //修改
        else if("modify".equals(action)){
            //获取参数
            String time = req.getParameter("addTime");
            SimpleDateFormat sf= new SimpleDateFormat("yyyy-MM-dd");
            Date addTime = null;
            try {
                addTime = sf.parse(time);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            String concernStatus = req.getParameter("concernStatus");
            String distance = req.getParameter("distance");
            String volume = req.getParameter("salesVolume");
            int salesVolume=0;
            if(volume!=null && !"".equals(volume)){
                salesVolume=Integer.parseInt(volume);
            }
            String shopAccount = req.getParameter("shopAccount");
            String shopAddress = req.getParameter("shopAddress");
            String shopCategoryList = req.getParameter("shopCategoryList");
            String shopCity = req.getParameter("shopCity");
            String shopCommunitySort = req.getParameter("shopCommunitySort");
            String shopDescribe = req.getParameter("shopDescribe");
            String shopDistrict = req.getParameter("shopDistrict");
            String shopGoodsList = req.getParameter("adshopGoodsListdTime");

            String id = req.getParameter("shopId");
            int shopId=0;
            if(id!=null && !"".equals(id)){
                shopId=Integer.parseInt(id);
            }
            String shopImage = req.getParameter("shopImage");
            String shopLatitude = req.getParameter("shopLatitude");
            String shopLongitude = req.getParameter("shopLongitude");
            String shopName = req.getParameter("shopName");
            String orderCount = req.getParameter("shopOrderCount");
            int shopOrderCount=0;
            if(orderCount!=null && !"".equals(orderCount)){
                shopOrderCount=Integer.parseInt(orderCount);
            }
            String shopPassword = req.getParameter("shopPassword");
            String shopPhone = req.getParameter("shopPhone");
            String shopProvince = req.getParameter("shopProvince");
            String shopSort = req.getParameter("shopSort");
            String status = req.getParameter("shopStatus");
            int shopStatus=0;
            if(status!=null && !"".equals(status)){
                shopStatus=Integer.parseInt(status);
            }
            String userId = req.getParameter("shopUserId");
            int shopUserId=0;
            if(userId!=null && !"".equals(userId)){
                shopUserId=Integer.parseInt(userId);
            }
            //参数封装
            Shop shop = new Shop( shopId,shopName,
                     shopDescribe,shopImage,shopPhone,
                     shopProvince,shopCity,shopDistrict,
                    shopAddress,shopLongitude,shopLatitude,
                    shopStatus,addTime,shopUserId,
                    shopAccount,shopPassword,shopSort,
                    salesVolume,distance,shopCommunitySort,
                    concernStatus,shopOrderCount,shopCategoryList,
                    shopGoodsList);
            //调用修改方法
            int num = shopDao.update(shop);
            Result res=null;
            if(num!=0){
                res=Result.success();
            }else {
                res=Result.error();
            }
            //返回前端
            resp.getWriter().write(JSON.toJSONString(res));
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
